/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.ClienteDao;
import java.util.List;
import model.Cliente;
import org.hibernate.Session;
import util.HibernateUtil;

/**
 *
 * @author Joel
 */
public class ClienteDaoImpl implements ClienteDao{

    @Override
    public void insertar(Cliente cliente) {
         Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            session.beginTransaction();
            session.save(cliente);
            session.beginTransaction().commit();
        }catch(Exception e)
        {
            System.out.println("Error en insertar Cliente \n"+e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void actualizar(Cliente cliente) {
         Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            session.beginTransaction();
            session.save(cliente);
            session.beginTransaction().commit();
        }catch(Exception e)
        {
            System.out.println("Error en actualizar Cliente \n"+e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void eliminar(Cliente cliente) {
         Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            session.beginTransaction();
            session.save(cliente);
            session.beginTransaction().commit();
        }catch(Exception e)
        {
            System.out.println("Error en eliminar Cliente\n"+e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Cliente buscarporId(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Cliente) session.load(Cliente.class, id);
    }

    @Override
    public List<Cliente> buscarTodos() {
        Session session = HibernateUtil.getSessionFactory().openSession();
       return session.createQuery("from Cliente").list();
    }
    
}
